const app = getApp();
Page({
  options: {
    addGlobalClass: true,
  },
  data: {
    is_android: true,
    is_hidden: true,
    loading: true,
    img: [
      "https://z3.ax1x.com/2021/05/30/2Vmaef.png",
      "https://z3.ax1x.com/2021/05/30/2VmQoD.png"
    ],
    ColorList: app.globalData.ColorList,
    color: 'red',
    loged: wx.getStorageSync('loged'),
    token: wx.getStorageSync('token'),
    user: wx.getStorageSync('user'),
    elements: [{
      title: '布局',
      name: 'layout',
      color: 'blue',
      icon: 'newsfill'
    }, ]
  },
  test(){
wx.cloud.callFunction({
  name:'sendMsg'
}).then(res=>{
  console.log(res)
})
  },
  modelCancel() {
    this.setData({
      is_hidden: true
    })
  },
  onLoad() {
    var res = wx.getSystemInfoSync()
    console.log(res.platform)
    if (res.platform == 'android') {
      this.setData({
        is_android: true,
        os: res.platform
      })
    }
    if (wx.getStorageSync('loged')) {
      this.getUserinfo()
      this.setData({
        loged: wx.getStorageSync('loged'),
        token: wx.getStorageSync('token'),
        user: wx.getStorageSync('user'),
        userinfo: wx.getStorageSync('userinfo'),
      })
      this.getPowerOrder()
    }

  },
  onShow() {
    var res = wx.getSystemInfoSync()
    console.log(res.platform)
    if (res.platform == 'android') {
      this.setData({
        is_android: true,
        os: res.platform
      })
    }
    if (wx.getStorageSync('loged')) {
      this.getUserinfo()
      this.setData({
        loged: wx.getStorageSync('loged'),
        token: wx.getStorageSync('token'),
        user: wx.getStorageSync('user'),
        userinfo: wx.getStorageSync('userinfo'),
      })
      this.getPowerOrder()
    }
  },
  getUserinfo() {
    wx.cloud.callFunction({
      name: "userInfo",
      data: {
        os: this.data.is_android ? 'android' : 'ios',
        userId: this.data.userId,
        token: this.data.token
      }
    }).then(res => {
      wx.hideLoading()
      console.log("成功", res)
      let bkdata = JSON.parse(res.result)
      if (bkdata.code == 0) {
        bkdata.data.amount = bkdata.data.amount.toFixed(2)
        this.setData({
          userinfo: bkdata.data
        })
        wx.setStorageSync('userinfo', bkdata.data)
      } else {
        wx.showToast({
          title: bkdata.msg,
          icon: 'none'
        })
      }
    }).catch(res => {
      console.log("失败", res)
      wx.showToast({
        title: '系统错误！',
        icon: 'none'
      })
    })
  },
  navtoOrder() {
    wx.navigateTo({
      url: '/pages/order/order',
    })
  },
  toAuth() {
    this.setData({
      is_hidden: false
    })
  },
  navtoPowerStation() {
    wx.switchTab({
      url: '/pages/user/user',
    })
  },
  navtoMe() {
    wx.navigateTo({
      url: '/pages/about/me',
    })
  },
  phoneInput(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  pwdInput(e) {
    this.setData({
      pwd: e.detail.value
    })
  },
  formatTimeTwo(number, format) {
    var dateTime = new Date(number)
    var year = dateTime.getFullYear();
    var month = dateTime.getMonth() + 1;
    var day = dateTime.getDate();
    var hour = dateTime.getHours();
    var minute = dateTime.getMinutes();
    var second = dateTime.getSeconds();
    var timeSpanStr = year + '-' + month + '-' + day + ' ' + hour + ':' + minute;
    return timeSpanStr;
  },
  getPowerOrder() {
    wx.cloud.callFunction({
      name: "getOrderinfo",
      data: {
        os: this.data.is_android ? 'android' : 'ios',
        userId: wx.getStorageSync('user').id,
        token: wx.getStorageSync('token'),
      }
    }).then(res => {
      wx.hideLoading()
      console.log("成功", res)
      let bkdata = JSON.parse(res.result)
      console.log(bkdata)
      if (bkdata.data[0].status == 0) {
        var alltime = bkdata.data[0].minutes
        var begintime = bkdata.data[0].beginTime
        var nowtime = Date.parse(new Date())
        var pop = (nowtime - begintime) / 60 / 1000 / alltime
        this.setData({
          pop: parseInt(pop),
          alltime,
          status: bkdata.data[0].status
        })
      } else {
        let a = this.formatTimeTwo(bkdata.data[0].beginTime)
        console.log(a)
        this.setData({
          status: bkdata.data[0].status,
          order: bkdata.data[0],
          a
        })
      }
    }).catch(res => {
      console.log("失败", res)
      wx.showToast({
        title: '系统错误！',
        icon: 'none'
      })
    })

  },
  onPullDownRefresh() {
    this.onLoad()
  },
  changeOs() {
    this.setData({
      is_android: !this.data.is_android
    })
  },
  toLogin() {
    if (!this.data.phone) {
      wx.showToast({
        title: '请输入手机号',
      })
    } else if (!this.data.pwd) {
      wx.showToast({
        title: '请输入登录密码',
      })
    } else {
      wx.showLoading({
        title: '加载中',
      })
      wx.cloud.callFunction({
        name: "login",
        data: {
          os: this.data.is_android ? 'android' : 'ios',
          phone: this.data.phone,
          pwd: this.data.pwd
        }
      }).then(res => {
        wx.hideLoading()
        console.log("成功", res)
        let bkdata = JSON.parse(res.result)
        if (bkdata.code == 0) {
          wx.showToast({
            title: '登录成功',
            icon: 'none'
          })
          wx.setStorageSync('token', bkdata.data.token)
          wx.setStorageSync('user', bkdata.data.user)
          wx.setStorageSync('loged', true)
          this.setData({
            is_hidden: true,
            user: bkdata.data.user,
            loged: wx.getStorageSync('loged')
          })
          wx.cloud.callFunction({
            name: "userInfo",
            data: {
              os: this.data.is_android ? 'android' : 'ios',
              userId: wx.getStorageSync('user').id,
              token: wx.getStorageSync('token'),
            }
          }).then(res => {
            wx.hideLoading()
            console.log("成功", res)
            let bkdata = JSON.parse(res.result)
            if (bkdata.code == 0) {
              bkdata.data.amount = bkdata.data.amount.toFixed(2)
              this.setData({
                userinfo: bkdata.data
              })
              wx.setStorageSync('userinfo', bkdata.data)
              this.getPowerOrder()
            } else {
              wx.showToast({
                title: bkdata.msg,
                icon: 'none'
              })
            }
          }).catch(res => {
            console.log("失败", res)
            wx.showToast({
              title: '系统错误！',
              icon: 'none'
            })
          })
        } else {
          wx.showToast({
            title: bkdata.msg,
            icon: 'none'
          })
        }
      }).catch(res => {
        console.log("失败", res)
        wx.showToast({
          title: '系统错误！',
          icon: 'none'
        })
      })
    }
  },
  share() {
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
  },
  onShareAppMessage: function (res) {
    return {
      title: '校园充电桩小助手来了！',
      path: '/pages/home/home'
    }
  }
})